﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Studio77.RuneMaster.Core;

namespace Studio77.RuneMaster.Pages
{
    public partial class StagePage : PhoneApplicationPage
    {
        public StagePage()
        {
            InitializeComponent();
        }

        string gameMode = "e";

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.NavigationService.Navigate(new Uri("/Pages/ModeSelectPage.xaml", UriKind.RelativeOrAbsolute));
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            NavigationContext.QueryString.TryGetValue("Mode", out gameMode);

            StageManager.Instance.CurrentGameMode = gameMode;

            //更新界面显示
            if (StageManager.Instance.CurrentGameMode == "e")
            {
                this.imgGameModeEasy.Visibility = System.Windows.Visibility.Visible;
                this.imgGameModeHard.Visibility = System.Windows.Visibility.Collapsed;
                this.imgGameModeNormal.Visibility = System.Windows.Visibility.Collapsed;
            }
            else if (StageManager.Instance.CurrentGameMode == "n")
            {
                this.imgGameModeEasy.Visibility = System.Windows.Visibility.Collapsed;
                this.imgGameModeHard.Visibility = System.Windows.Visibility.Collapsed;
                this.imgGameModeNormal.Visibility = System.Windows.Visibility.Visible;
            }
            else if (StageManager.Instance.CurrentGameMode == "h")
            {
                this.imgGameModeEasy.Visibility = System.Windows.Visibility.Collapsed;
                this.imgGameModeHard.Visibility = System.Windows.Visibility.Visible;
                this.imgGameModeNormal.Visibility = System.Windows.Visibility.Collapsed;
            }

            int stageUnlockCount = 0;

            //清除页面堆栈
            try
            {
                for (int i = 0; i < 4; i++)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            catch
            { }

            //获取该Mode下的关卡完成情况
            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 1)
                && StageManager.Instance.StageUnlockStatus[gameMode + "_" + 1])
            {
                stageUnlockCount++;
                btnStage1.IsEnabled = true;
            }
            else
            {
                btnStage1.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 2)
    && StageManager.Instance.StageUnlockStatus[gameMode + "_" + 2])
            {
                stageUnlockCount++;
                btnStage2.IsEnabled = true;
            }
            else
            {
                btnStage2.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 3)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 3])
            {
                stageUnlockCount++;
                btnStage3.IsEnabled = true;
            }
            else
            {
                btnStage3.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 4)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 4])
            {
                stageUnlockCount++;
                btnStage4.IsEnabled = true;
            }
            else
            {
                btnStage4.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 5)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 5])
            {
                stageUnlockCount++;
                btnStage5.IsEnabled = true;
            }
            else
            {
                btnStage5.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 6)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 6])
            {
                stageUnlockCount++;
                btnStage6.IsEnabled = true;
            }
            else
            {
                btnStage6.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 7)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 7])
            {
                stageUnlockCount++;
                btnStage7.IsEnabled = true;
            }
            else
            {
                btnStage7.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 8)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 8])
            {
                stageUnlockCount++;
                btnStage8.IsEnabled = true;
            }
            else
            {
                btnStage8.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 9)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 9])
            {
                stageUnlockCount++;
                btnStage9.IsEnabled = true;
            }
            else
            {
                btnStage9.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 10)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 10])
            {
                stageUnlockCount++;
                btnStage10.IsEnabled = true;
            }
            else
            {
                btnStage10.IsEnabled = false;
            }

            ////如果大于等于6个关卡被解锁，则开放下个模式
            //if (stageUnlockCount >= 6)
            //{
            //    if (StageManager.Instance.CurrentGameMode == "e")
            //    {
            //        StageManager.Instance.ModeUnlockStatus["n"] = true;
            //    }
            //    else if (StageManager.Instance.CurrentGameMode == "n")
            //    {
            //        StageManager.Instance.ModeUnlockStatus["h"] = true;
            //    }
            //    StageManager.Instance.SaveModeUnlockInfo();
            //}

        }


        private void btnStage_Click_1(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                SoundManager.PlayEffect(SoundManager.buttonClick);
                string stageIndex = btn.Tag.ToString();
                StageManager.Instance.CurrentStageIndex = stageIndex;
                //跳转到道具选择页面
                this.NavigationService.Navigate(new Uri("/Pages/ToolChoosePage.xaml", UriKind.RelativeOrAbsolute));
            }
        }
    }
}